﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Net;
using System.Xml.Serialization;
using System.IO;
using Galaktika.BI.Silverlight;

namespace Galaktika.BI.Extensibility.Dom.Dac
{
    [XmlRoot("DalRootContainer",
        Namespace = Galaktika.BI.Silverlight.Common.Namespace)]
    public class BrickDacContainer
    {
        public BrickDacContainer()
        {
            m_Contexts = new List<BrickBindingContext>();
            m_DataSources = new List<BrickDataSource>();
            m_Variables = new List<BrickVariable>();
        }

        public BrickDacContainer(string ss)
            : this()
        {
            var ds = new BrickDataSource(
                "ds1", 
                new BrickQuery("query1", "select1", "update1"),
                new BrickConnection("connection1", "", DataProvider.Olap, "conn str 1", "pump 1"));
            m_DataSources.Add(ds);
        }

        List<BrickBindingContext> m_Contexts;
        [XmlArray]
        [XmlArrayItem("DalContext", typeof(BrickBindingContext))]
        public List<BrickBindingContext> Contexts
        {
            get
            {
                return m_Contexts;
            }

        }

        List<BrickDataSource> m_DataSources;
        [XmlArray]
        [XmlArrayItem("DalDataSource", typeof(BrickDataSource))]
        public List<BrickDataSource> DataSources
        {
            get
            {
                return m_DataSources;
            }
        }

        List<BrickVariable> m_Variables;
        [XmlArray]
        [XmlArrayItem("DalVariable", typeof(BrickVariable))]
        public List<BrickVariable> Variables
        {
            get
            {
                return m_Variables;
            }
        }

        public string ToXml()
        {
            return XmlUtility.Obj2XmlStr(this);
        }

        public static BrickDacContainer FromStream(Stream stream)
        {
            using (StreamReader sr = new StreamReader(stream))
            {
                string xml = sr.ReadToEnd();
                return BrickObject.FromXml<BrickDacContainer>(xml);
            }
        }
    }
}
